﻿using OperationPlugins.Compositions;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace OperationPlugins
{
    public abstract class ContextualPlugin
    {
        /// <summary>
        /// Gets and sets the composition container.
        /// </summary>
        private static CompositionContainer CompositionContainer { set; get; }

        /// <summary>
        /// Gets and sets the plugin input value.
        /// </summary>
        protected virtual object Value { set; get; }

        /// <summary>
        /// Gets and sets the operation context.
        /// </summary>
        protected virtual OperationContext Context { set; get; }

        /// <summary>
        /// Gets the plugin.
        /// </summary>
        /// <typeparam name="T">The type of the plugin object to return.</typeparam>
        /// <returns>The plugin object, or null if not found.</returns>
        protected virtual T GetPlugin<T>()
        {
            return CompositionContainer.GetPlugin<T>();
        }

        /// <summary>
        /// Gets all plugins.
        /// </summary>
        /// <typeparam name="T">The type of the plugin objects to return.</typeparam>
        /// <returns>Plugin objects, or an empty collection if not found.></returns>
        protected virtual IEnumerable<T> GetPlugins<T>()
        {
            return CompositionContainer.GetPlugins<T>();
        }

        /// <summary>
        /// Initialize. 
        /// </summary>
        [Export(typeof(IInitialize))]
        private sealed class Initializer : IInitialize
        {
            void IInitialize.Initialize(InitializationContext context)
            {
                CompositionContainer = context.CompositionContainer;
            }
        }

    }
}
